package com.berrontech.asbog;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Objects;

/**
 * Create by levent8421 2021/4/8 17:57
 * DashboardActivity
 * Dashboard
 *
 * @author levent8421
 */
public class DashboardActivity
        extends AppCompatActivity
        implements View.OnClickListener,
        View.OnLongClickListener,
        SimpleWebChromeClient.WebViewProgressListener {
    private static final String PASSWORD = "123456";
    private static final String TAG = "DashboardActivity";
    public static final String WEB_VIEW_URL = "dashboard.url";
    private String url;
    private TextView tvInfo;
    private WebView wvMain;
    private ProgressBar pbLoadingProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard);
        loadUrl();
        Log.d(TAG, "WebView url=" + url);
        initView();
        startWeb();
        hideNavigation();
    }

    private void hideNavigation() {
        try {
            final int res = NavigationBarUtils.hideNavigation(this);
            if (res < 0) {
                throw new Exception("Cmd Res Code=" + res);
            }
        } catch (Exception e) {
            Toast.makeText(this, "Can not hide navigation for this device:" + e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initView() {
        tvInfo = findViewById(R.id.tvInfo);
        findViewById(R.id.btnReload).setOnClickListener(this);
        final ImageButton btnBack = findViewById(R.id.btnBack);
        btnBack.setOnClickListener(this);
        btnBack.setOnLongClickListener(this);

        wvMain = findViewById(R.id.wvMain);
        wvMain.setWebViewClient(new SimpleWebViewClient());
        wvMain.setWebChromeClient(new SimpleWebChromeClient(this));
        final WebSettings webSettings = wvMain.getSettings();
        webSettings.setJavaScriptEnabled(true);
        pbLoadingProgress = findViewById(R.id.pbLoadingProgress);
        final ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
    }

    private void startWeb() {
        tvInfo.setText(url);
        wvMain.loadUrl(url);
    }

    private void loadUrl() {
        final Intent intent = getIntent();
        url = intent.getStringExtra(WEB_VIEW_URL);
    }

    private void showExitDialog() {
        final EditText passwordInput = new EditText(this);
        passwordInput.setText("");
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("请输入密码")
                .setPositiveButton("确定", (dialog, which) -> {
                    final String input = passwordInput.getText().toString().trim();
                    if (Objects.equals(input, PASSWORD)) {
                        dialog.dismiss();
                        DashboardActivity.this.finish();
                    } else {
                        Toast.makeText(DashboardActivity.this, "密码错误", Toast.LENGTH_LONG).show();
                    }
                })
                .setNegativeButton("取消", (dialog, which) -> dialog.dismiss())
                .setView(passwordInput);
        builder.show();
    }

    @Override
    public void onClick(View v) {
        if (Objects.equals(R.id.btnReload, v.getId())) {
            wvMain.reload();
            Toast.makeText(this, "Reloading...", Toast.LENGTH_SHORT).show();
        } else if (Objects.equals(R.id.btnBack, v.getId())) {
            onBack();
        }
    }

    private void onBack() {
        if (!wvMain.canGoBack()) {
            showExitDialog();
            return;
        }
        wvMain.goBack();
    }

    @Override
    public void onBackPressed() {
        onBack();
    }

    @Override
    public boolean onLongClick(View v) {
        if (Objects.equals(v.getId(), R.id.btnBack)) {
            showExitDialog();
            return true;
        }
        return false;
    }

    @Override
    public void onProgressChanged(WebView view, int progress, boolean loaded) {
        if (loaded) {
            pbLoadingProgress.setVisibility(View.GONE);
        } else {
            pbLoadingProgress.setVisibility(View.VISIBLE);
            pbLoadingProgress.setProgress(progress);
        }
    }
}